package cn.lil.servlet;

import cn.lil.dao.OrderDao;
import cn.lil.dao.OrderItemDao;
import cn.lil.model.Bearing;
import cn.lil.model.Order;
import cn.lil.model.OrderItem;
import cn.lil.model.OrderItemWithBearing;
import cn.lil.service.BearingService;
import cn.lil.service.OrderService;
import com.oracle.wls.shaded.org.apache.xpath.operations.Or;
import jakarta.servlet.*;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.*;

import java.io.IOException;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "OrderItemListServlet", value = "/OrderItemListServlet")
public class OrderItemListServlet extends HttpServlet {
    private OrderService orderService = new OrderService();
    private BearingService bearingService = new BearingService();
    private OrderItemDao orderItemDao = new OrderItemDao();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Order o = (Order) request.getSession().getAttribute("order");
        System.out.println(o.getId() + "   o的值");
        try {
            o.setItemList(orderItemDao.getItemByOrderId(o.getId()));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        List<OrderItemWithBearing> orderItemWithBearingList = new ArrayList<>();
        for (OrderItem item : o.getItemList()) {
            try {
                Bearing bearing = bearingService.selectBearingById(item.getBearingId());
                float bearingPrice = 0.00f;
                if (bearing != null) {
                    bearingPrice = bearing.getPrice();
                    orderItemWithBearingList.add(new OrderItemWithBearing(item, bearing.getType(), bearingPrice, bearing.getImgSrc()));
                } else {
                    // 处理轴承不存在的情况
                    orderItemWithBearingList.add(new OrderItemWithBearing(item, "未知类型", null, null));
                }
            } catch (SQLException e) {
                e.printStackTrace();
                // 处理单个轴承查询异常
                orderItemWithBearingList.add(new OrderItemWithBearing(item, "查询错误", null, null));
            }
        }
        System.out.println(orderItemWithBearingList);
        request.setAttribute("orderItemWithBearingList", orderItemWithBearingList);
        request.setAttribute("orderItemList", o.getItemList());
        //        将查询到item的order重新放入到会话中
        request.getSession().setAttribute("order", o);
        request.getRequestDispatcher("cart.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Order o = (Order) request.getSession().getAttribute("order");
        System.out.println(o.getId() + "   o的值");
        try {
            o.setItemList(orderItemDao.getItemByOrderId(o.getId()));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        List<OrderItemWithBearing> orderItemWithBearingList = new ArrayList<>();
        for (OrderItem item : o.getItemList()) {
            try {
                Bearing bearing = bearingService.selectBearingById(item.getBearingId());
                float bearingPrice = 0.00f;
                if (bearing != null) {
                    bearingPrice = bearing.getPrice();
                    orderItemWithBearingList.add(new OrderItemWithBearing(item, bearing.getType(), bearingPrice, bearing.getImgSrc()));
                } else {
                    // 处理轴承不存在的情况
                    orderItemWithBearingList.add(new OrderItemWithBearing(item, "未知类型", null, null));
                }
            } catch (SQLException e) {
                e.printStackTrace();
                // 处理单个轴承查询异常
                orderItemWithBearingList.add(new OrderItemWithBearing(item, "查询错误", null, null));
            }
        }
        System.out.println(orderItemWithBearingList);
        request.setAttribute("orderItemWithBearingList", orderItemWithBearingList);
        request.setAttribute("orderItemList", o.getItemList());
//        将查询到item的order重新放入到会话中
        request.getSession().setAttribute("order", o);
        request.getRequestDispatcher("cart.jsp").forward(request, response);
    }
}
